/**
 * Copyright 2012 Ricardo Engel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mookt.dcubed;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ricardo Engel
 * 
 */
public abstract class InMemoryRepository<T extends AggregateRoot<ID>, ID extends Serializable>
        implements Repository<T, ID> {

    Map<ID, T> aggregateRoots = new HashMap<ID, T>();

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.dcubed.Repository#save(org.mookt.dcubed.AggregateRoot)
     */
    public void save(T entity) {
        aggregateRoots.put(entity.getId(), entity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.dcubed.Repository#findOne(java.io.Serializable)
     */
    public T find(ID id) {
        return aggregateRoots.get(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.dcubed.Repository#exists(java.io.Serializable)
     */
    public boolean exists(ID id) {
        return aggregateRoots.containsKey(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.dcubed.Repository#count()
     */
    public long count() {
        return aggregateRoots.size();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.dcubed.Repository#delete(java.io.Serializable)
     */
    public void delete(ID id) {
        aggregateRoots.remove(id);
    }

}
